 <template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="110px">
      <el-form-item label="场所名称" prop="placeName">
        <el-input
          v-model="queryParams.placeName"
          placeholder="请输入场所名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="地址" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系人" prop="contactPerson">
        <el-input
          v-model="queryParams.contactPerson"
          placeholder="请输入联系人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系电话" prop="phoneNumber">
        <el-input
          v-model="queryParams.phoneNumber"
          placeholder="请输入联系电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:place:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:place:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:place:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['business:place:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="placeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="场所名称" align="center" prop="placeName" />
      <el-table-column label="场所类型" align="center" prop="placeType" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.bus_place_type" :value="scope.row.placeType"/>
        </template>
      </el-table-column>
      <el-table-column label="地址" align="center" prop="address" />
      <el-table-column label="联系人" align="center" prop="contactPerson" >
        <template slot-scope="scope" >
          <span >{{ scope.row.contactPerson }}</span><br>
          <span >{{ scope.row.phoneNumber}}</span>
        </template>
      </el-table-column>
      <el-table-column label="签约状态" align="center" prop="status"  >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.bus_place_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="电梯情况" align="center" prop="contactPerson" >
        <template slot-scope="scope" >
          <span >{{ '台数：' + scope.row.elevatorNum +' 台'}}</span><br>
          <span >{{ '保养金额： ' + scope.row.amount + '元 / 年'}}</span>
        </template>
      </el-table-column>
      <el-table-column label="排序" align="center" prop="sortOrder" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-info"
            @click="handleInfo(scope.row)"
            v-hasPermi="['business:place:query']"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:place:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:place:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改场所信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="90%" append-to-body :close-on-click-modal="false">
      <el-tabs tab-position="left" >
        <el-tab-pane label="场所信息">
          <el-divider content-position="center">场所基本信息</el-divider>
          <el-form ref="form" :model="form" :rules="rules" label-width="120px">
            <el-row :gutter="20">
              <el-col :span="12">
                <el-form-item label="场所名称" prop="placeName">
                  <el-input v-model="form.placeName" placeholder="请输入场所名称" />
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="场所类型" prop="type">
                  <el-select v-model="form.placeType" placeholder="请输入场所类型" clearable>
                    <el-option
                      v-for="dict in dict.type.bus_place_type"
                      :key="dict.value"
                      :label="dict.label"
                      :value="dict.value"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="排序字段" prop="sortOrder">
                  <el-input-number v-model="form.sortOrder" placeholder="请输入排序字段" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="签约状态" prop="sortOrder">
                  <el-radio-group v-model="form.status">
                    <el-radio  v-for="dict in dict.type.bus_place_status"  :label="dict.label">{{dict.label}}</el-radio>
                  </el-radio-group>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="联系人" prop="contactPerson">
                  <el-input v-model="form.contactPerson" placeholder="请输入联系人" />
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="联系电话" prop="phoneNumber">
                  <el-input v-model="form.phoneNumber" placeholder="请输入联系电话" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="所属区域" prop="province">
                  <el-cascader
                    v-model="areaValue"
                    :options="areaOptions"
                    :props="{ expandTrigger: 'hover' }"
                    @change="handleChangeArea"></el-cascader>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="详情地址" prop="address">
                  <el-input v-model="form.address" placeholder="请输入地址" />
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="纬度" prop="latitude">
                  <el-input v-model="form.latitude" placeholder="请输入纬度" />
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="经度" prop="longitude">
                  <el-input v-model="form.longitude" placeholder="请输入经度" />
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="图片集合" prop="images">
                  <image-upload v-model="form.images"/>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="内容" prop="richText">
                  <el-input v-model="form.richText" type="textarea" placeholder="请输入内容" />
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="备注" prop="remark">
                  <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </el-tab-pane>
        <el-tab-pane label="电梯信息">
          <el-divider content-position="center">电梯基本信息</el-divider>
          <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
              <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddYtElevatorInfo">添加</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteYtElevatorInfo">删除</el-button>
            </el-col>
          </el-row>
          <el-table :data="ytElevatorInfoList" :row-class-name="rowYtElevatorInfoIndex" @selection-change="handleYtElevatorInfoSelectionChange" ref="ytElevatorInfo" show-summary :summary-method="getSummaries" border>
            <el-table-column type="selection" width="50" align="center" />
            <el-table-column label="操作" prop="sortOrder" width="100" fixed>
              <template slot-scope="scope">
                <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleCopyYtElevatorInfo(scope.row)" round>复制</el-button>
              </template>
            </el-table-column>
            <el-table-column label="序号" align="center" prop="index" width="80"  fixed/>
            <el-table-column label="注册代码" prop="registrationCode" width="200"  fixed>
              <template slot-scope="scope">
                <el-input v-model="scope.row.registrationCode" placeholder="请输入注册代码" size="small"/>
              </template>
            </el-table-column>
            <el-table-column label="台数" prop="num" width="150">
              <template slot-scope="scope">
                <el-input-number v-model="scope.row.num" placeholder="请输入台数" controls-position="right" size="mini" :min="1"/>
              </template>
            </el-table-column>
            <el-table-column label="电梯出厂编号" prop="factoryNumber" width="160" fixed>
              <template slot-scope="scope">
                <el-input v-model="scope.row.factoryNumber" placeholder="请输入电梯出厂编号" size="small"/>
              </template>
            </el-table-column>
            <el-table-column label="电梯品牌" prop="brand" width="150" fixed>
              <template slot-scope="scope">
                <el-autocomplete
                  class="inline-input"
                  v-model="scope.row.brand"
                  :fetch-suggestions="querySearchBrand"
                  placeholder="请输入电梯品牌"
                  @select="handleSelectBrand"
                ></el-autocomplete>
              </template>
            </el-table-column>
            <el-table-column label="型号" prop="model" width="200" fixed>
              <template slot-scope="scope">
                <el-autocomplete
                  class="inline-input"
                  v-model="scope.row.model"
                  :fetch-suggestions="querySearchModel"
                  placeholder="请输入型号"
                  @select="handleSelectModel"
                ></el-autocomplete>
              </template>
            </el-table-column>
            <el-table-column label="运行地点" prop="detailedLocation" width="200">
              <template slot-scope="scope">
                <el-input v-model="scope.row.detailedLocation" placeholder="请输入详细地点" size="small"/>
              </template>
            </el-table-column>
            <el-table-column label="层数" prop="floors"  width="150">
              <template slot-scope="scope">
                <el-input-number v-model="scope.row.floors" placeholder="请输入层数" controls-position="right" size="mini"/>
              </template>
            </el-table-column>
            <el-table-column label="站数" prop="stops"  width="150">
              <template slot-scope="scope">
                <el-input-number v-model="scope.row.stops" placeholder="请输入站数" controls-position="right" size="mini"/>
              </template>
            </el-table-column>
            <el-table-column label="门数" prop="doors" width="150">
              <template slot-scope="scope">
                <el-input-number v-model="scope.row.doors" placeholder="请输入门数" controls-position="right" size="mini"/>
              </template>
            </el-table-column>
            <el-table-column label="保养金额（元/台/月）" prop="maintainAmount" width="180">
              <template slot-scope="scope">
                <el-input-number v-model="scope.row.maintainAmount" placeholder="请输入门数" controls-position="right" size="mini" :min="0"/>
              </template>
            </el-table-column>
            <el-table-column label="电梯状态" prop="status" width="150">
              <template slot-scope="scope">
                <el-select v-model="scope.row.status" placeholder="请选择类型" clearable size="small">
                  <el-option
                    v-for="dict in dict.type.bus_elevator_status"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                  />
                </el-select>
              </template>
            </el-table-column>
            <el-table-column label="备注" prop="remark" >
              <template slot-scope="scope">
                <el-input v-model="scope.row.remark" placeholder="请输入备注" size="small"/>
              </template>
            </el-table-column>
            <el-table-column label="排序字段" prop="sortOrder" >
              <template slot-scope="scope">
                <el-input-number v-model="scope.row.sortOrder" placeholder="请输入排序字段" size="mini" :min="0"/>
              </template>
            </el-table-column>
            <el-table-column label="安装日期" prop="installedDate" >
              <template slot-scope="scope">
                <el-date-picker clearable v-model="scope.row.installedDate" type="date" value-format="yyyy-MM-dd" placeholder="请选择安装日期" size="small"/>
              </template>
            </el-table-column>
            <el-table-column label="上次维护日期" prop="lastMaintenanceDate" >
              <template slot-scope="scope">
                <el-date-picker clearable v-model="scope.row.lastMaintenanceDate" type="date" value-format="yyyy-MM-dd" placeholder="请选择上次维护日期" size="small"/>
              </template>
            </el-table-column>
          </el-table>
          <span style="color: red">注意：更多电梯信息字段需滑动底部滚动条查看</span>
        </el-tab-pane>
      </el-tabs>

      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 查看场所信息对话框 -->
    <el-dialog :title="title" :visible.sync="openInfo" width="90%" append-to-body :close-on-click-modal="false">
      <el-divider content-position="center">场所基本信息</el-divider>
      <el-descriptions border>
        <el-descriptions-item label="场所名称">{{ form.placeName }}</el-descriptions-item>
        <el-descriptions-item label="场所类型">
          <dict-tag :options="dict.type.bus_place_type" :value="form.placeType"/>
        </el-descriptions-item>
        <el-descriptions-item label="地址">{{ form.address }}</el-descriptions-item>
        <el-descriptions-item label="联系人">{{ form.contactPerson }} / {{ form.phoneNumber }} </el-descriptions-item>
        <el-descriptions-item label="签约状态">
          <dict-tag :options="dict.type.bus_place_status" :value="form.status"/>
        </el-descriptions-item>
      </el-descriptions>
      <el-divider content-position="center">电梯基本信息</el-divider>
      <el-table :data="ytElevatorInfoList"  ref="ytElevatorInfo" show-summary :summary-method="getSummaries" border>
        <el-table-column label="注册代码" prop="registrationCode" width="200"  fixed></el-table-column>
        <el-table-column label="台数" prop="num" width="150"></el-table-column>
        <el-table-column label="电梯出厂编号" prop="factoryNumber" width="160" fixed></el-table-column>
        <el-table-column label="电梯品牌" prop="brand" width="150" fixed></el-table-column>
        <el-table-column label="型号" prop="model" width="200" fixed></el-table-column>
        <el-table-column label="运行地点" prop="detailedLocation" width="200"></el-table-column>
        <el-table-column label="层数" prop="floors"  width="150"></el-table-column>
        <el-table-column label="站数" prop="stops"  width="150"></el-table-column>
        <el-table-column label="门数" prop="doors" width="150"></el-table-column>
        <el-table-column label="保养金额（元/台/月）" prop="maintainAmount" width="180"></el-table-column>
        <el-table-column label="电梯状态" prop="status" width="150">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.bus_elevator_status" :value="scope.row.status"/>
          </template>
        </el-table-column>
        <el-table-column label="备注" prop="remark" show-overflow-tooltip></el-table-column>
        <el-table-column label="安装日期" prop="installedDate" >
          <template slot-scope="scope">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.installedDate, '{y}-{m}-{d}') }}</span>
            </template>
          </template>
        </el-table-column>
        <el-table-column label="上次维护日期" prop="lastMaintenanceDate" >
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.lastMaintenanceDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
      </el-table>
      <span style="color: red">注意：更多电梯信息字段需滑动底部滚动条查看</span>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPlace, getPlace, delPlace, addPlace, updatePlace } from "@/api/business/place";
import {listArea} from "@/api/business/area";

export default {
  name: "Place",
  dicts: ['bus_place_type','bus_place_status','bus_elevator_status'],
  data() {
    return {
      // 电梯品牌选项
      brandsRestaurants: [],
      // 电梯品牌类型选项
      modelRestaurants: [],
      // 地域选项
      areaOptions: [],
      // 子表选中数据
      checkedYtElevatorInfo: [],
      // 区域选项值
      areaValue:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 场所信息表格数据
      placeList: [],
      // 电梯信息，用于存储电梯的基本信息及其所属场所表格数据
      ytElevatorInfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openInfo: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        parentId: null,
        placeName: null,
        placeType: null,
        province: null,
        city: null,
        district: null,
        town: null,
        address: null,
        village: null,
        contactPerson: null,
        phoneNumber: null,
        latitude: null,
        longitude: null,
        images: null,
        richText: null,
        sortOrder: 0,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        placeName: [
          { required: true, message: "场所名称不能为空", trigger: "blur" }
        ],
        placeType: [
          { required: true, message: "场所类型不能为空", trigger: "change" }
        ],
        address: [
          { required: true, message: "地址不能为空", trigger: "blur" }
        ],
        village: [
          { required: true, message: "社区或村不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getAreaList();
  },
  methods: {
    /** 查询场所信息列表 */
    getList() {
      this.loading = true;
      listPlace(this.queryParams).then(response => {
        this.placeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询地域选项信息 */
    getAreaList(){
      listArea({pageSize: 10000}).then(response => {
        this.areaOptions = this.handleTree(response.data, "id", "parentId");
      });
    },

    // 选择地域选项值
    handleChangeArea(value){
      this.areaValue = value;
      this.form.area = this.areaValue.join(',')
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openInfo = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        placeId: null,
        parentId: null,
        placeName: null,
        placeType: null,
        province: null,
        city: null,
        district: null,
        town: null,
        address: null,
        area: '',
        village: null,
        contactPerson: null,
        phoneNumber: null,
        latitude: null,
        longitude: null,
        images: null,
        richText: null,
        sortOrder: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        status: '已签约',
        delFlag: null
      };
      this.ytElevatorInfoList = [];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.placeId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加场所信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const placeId = row.placeId || this.ids
      getPlace(placeId).then(response => {
        this.form = response.data;
        this.ytElevatorInfoList = response.data.ytElevatorInfoList;
        if (typeof this.form.area === 'string') {
          this.areaValue = this.form.area.split(',').map(item => item.trim());
        } else {
          this.areaValue = [];
        }
        this.open = true;
        this.title = "修改场所信息";
      });
    },
    /** 修改按钮操作 */
    handleInfo(row) {
      this.reset();
      const placeId = row.placeId || this.ids
      getPlace(placeId).then(response => {
        this.form = response.data;
        this.ytElevatorInfoList = response.data.ytElevatorInfoList;
        if (typeof this.form.area === 'string') {
          this.areaValue = this.form.area.split(',').map(item => item.trim());
        } else {
          this.areaValue = [];
        }
        this.openInfo = true;
        this.title = "场所信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          // 检查注册代码是否重复
          const registrationCodes = new Map();
          let duplicateIndexes = [];
          this.ytElevatorInfoList.forEach((elevator, index) => {
            if (registrationCodes.has(elevator.registrationCode)) {
              const existingIndex = registrationCodes.get(elevator.registrationCode);
              duplicateIndexes.push([existingIndex + 1, index + 1]); // 行号从1开始
            } else {
              registrationCodes.set(elevator.registrationCode, index);
            }
          });

          if (duplicateIndexes.length > 0) {
            const duplicateMessages = duplicateIndexes.map(([index1, index2]) => `第 ${index1} 行和第 ${index2} 行`);
            this.$modal.msgError(`注册代码在以下行号重复: ${duplicateMessages.join(', ')}`);
            return; // 阻止表单提交
          }
          this.form.ytElevatorInfoList = this.ytElevatorInfoList;
          if (this.form.placeId != null) {
            updatePlace(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPlace(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 电梯信息，用于存储电梯的基本信息及其所属场所序号 */
    rowYtElevatorInfoIndex({ row, rowIndex }) {
      row.index = rowIndex + 1;
    },
    /** 电梯信息，用于存储电梯的基本信息及其所属场所添加按钮操作 */
    handleAddYtElevatorInfo() {
      if (!this.ytElevatorInfoList) {
        this.ytElevatorInfoList = [];
      }
      let obj = {};
      obj.serialNumber = "";
      obj.registrationCode = "";
      obj.factoryNumber = "";
      obj.floors = "";
      obj.stops = "";
      obj.doors = "";
      obj.detailedLocation = "";
      obj.latitude = "";
      obj.longitude = "";
      obj.brand = "";
      obj.model = "";
      obj.installedDate = "";
      obj.lastMaintenanceDate = "";
      obj.status = "正常运行";
      obj.sortOrder = "";
      obj.remark = "";
      obj.num = 1;
      obj.maintainAmount = 0;
      this.ytElevatorInfoList.push(obj);
    },
    // 复制上一行电梯信息
    handleCopyYtElevatorInfo(elevator){
      let obj = {};
      obj.serialNumber = elevator.serialNumber;
      obj.registrationCode = elevator.registrationCode;
      obj.factoryNumber = elevator.factoryNumber;
      obj.floors = elevator.floors;
      obj.stops = elevator.stops;
      obj.doors = elevator.doors ;
      obj.detailedLocation = elevator.detailedLocation ;
      obj.latitude = elevator.latitude;
      obj.longitude =elevator.longitude;
      obj.brand = elevator.brand;
      obj.model = elevator.model ;
      obj.installedDate = elevator.installedDate;
      obj.lastMaintenanceDate = elevator.lastMaintenanceDate;
      obj.status = "正常运行";
      obj.sortOrder = elevator.sortOrder;
      obj.remark = elevator.remark;
      obj.num = elevator.num;
      obj.maintainAmount = elevator.maintainAmount;
      this.ytElevatorInfoList.push(obj);
    },
    /** 电梯信息，用于存储电梯的基本信息及其所属场所删除按钮操作 */
    handleDeleteYtElevatorInfo() {
      if (this.checkedYtElevatorInfo.length == 0) {
        this.$modal.msgError("请先选择要删除的电梯信息，用于存储电梯的基本信息及其所属场所数据");
      } else {
        const ytElevatorInfoList = this.ytElevatorInfoList;
        const checkedYtElevatorInfo = this.checkedYtElevatorInfo;
        this.ytElevatorInfoList = ytElevatorInfoList.filter(function(item) {
          return checkedYtElevatorInfo.indexOf(item.index) == -1
        });
      }
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const placeIds = row.placeId || this.ids;
      this.$modal.confirm('是否确认删除场所信息编号为"' + placeIds + '"的数据项？').then(function() {
        return delPlace(placeIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 复选框选中数据 */
    handleYtElevatorInfoSelectionChange(selection) {
      this.checkedYtElevatorInfo = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/place/export', {
        ...this.queryParams
      }, `场所信息_${new Date().getTime()}.xlsx`)
    },
    querySearchBrand(queryString, cb) {
      var brandsRestaurants = this.brandsRestaurants;
      var results = queryString ? brandsRestaurants.filter(this.createFilter(queryString)) : brandsRestaurants;
      console.log("results",results);
      // 调用 callback 返回建议列表的数据
      cb(results);
    },
    querySearchModel(queryString, cb) {
      var modelRestaurants = this.modelRestaurants;
      var results = queryString ? modelRestaurants.filter(this.createFilter(queryString)) : modelRestaurants;
      // 调用 callback 返回建议列表的数据
      cb(results);
    },
    createFilter(queryString) {
      return (restaurant) => {
        return (restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0);
      };
    },
    loadAllBrand() {
      // 判断本地缓存是否有品牌数据
      if (localStorage.getItem('brands') ) {
        return JSON.parse(localStorage.getItem('brands'));
      } else {
        return [
          { "value": "日立电梯"},
          { "value": "三菱电梯"},
          { "value": "东芝电梯"},
          { "value": "富士达电梯"},
          { "value": "现代电梯"},
          { "value": "奥的斯电梯"},
          { "value": "迅达电梯"},
          { "value": "通力电梯"},
          { "value": "蒂升电梯"},
          { "value": "其他电梯"},
        ]
      }
    },
    loadAllModel() {
      // 判断本地缓存是否有电梯品牌型号数据
      if (localStorage.getItem('brands_models')) {
        return JSON.parse(localStorage.getItem('brands_models'));
      } else {
        return []
      }
    },
    handleSelectBrand(item) {

    },
    handleSelectModel(item) {

    },
    // 添加电梯信息合计
    getSummaries(param){
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 2) {
          sums[index] = '合计';
          return;
        }
        // 仅对 num 和 maintainAmount 字段进行计算
        if (column.property === 'num' || column.property === 'maintainAmount') {
          const values = data.map(item => Number(item[column.property]));
          if (!values.every(value => isNaN(value)) ) {
            const sum = values.reduce((prev, curr) => {
              const value = Number(curr);
              if (!isNaN(value)) {
                return prev + curr;
              } else {
                return prev;
              }
            }, 0);
            if (column.property === 'num') {
              sums[3] = `电梯共： ${sum} 台`;
            } else if (column.property === 'maintainAmount') {
              sums[5] = `保养金额： ${sum * 12} 元 / 年`;
            }
          } else {
            sums[index] = '';
          }
        }
      });

      return sums;
    }
  },
  mounted() {
    this.brandsRestaurants = this.loadAllBrand();
    this.modelRestaurants = this.loadAllModel();
  }
};
</script>
